package com.yy.wga.user.service.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.yy.wga.common.util.mapping.BeansUtils;
import com.yy.wga.user.feign.request.UserRelationListReq;
import com.yy.wga.user.feign.response.UserRelationListResp;
import com.yy.wga.user.service.mapper.UserBaseMapper;
import com.yy.wga.user.service.mapper.UserRelationMapper;
import com.yy.wga.user.service.pojo.UserBaseDO;
import com.yy.wga.user.service.pojo.UserRelationDO;
import com.yy.wga.user.service.service.UserRelationService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.function.Function;
import java.util.stream.Collectors;

/**
 * 直播用户锁粉关系(UserRelation)表服务实现类
 *
 * @author guochao
 * @since 2022-07-27 22:12:32
 */
@Slf4j
@Service
public class UserRelationServiceImpl extends ServiceImpl<UserRelationMapper, UserRelationDO> implements UserRelationService {
    @Resource
    private UserBaseMapper userBaseMapper;

    @Override
    public void removeByUserId(Long userId) {
        baseMapper.removeByUserId(userId);
    }

    @Override
    public void removeByParentUserId(Long parentUserId) {
        baseMapper.removeByParentUserId(parentUserId);
    }

    @Override
    public List<UserRelationListResp> listData(UserRelationListReq request) {
        PageHelper.startPage(request.getPageNum(), request.getPageSize(), false);
        LambdaQueryWrapper<UserRelationDO> userRelationDOLambdaQueryWrapper = new LambdaQueryWrapper<UserRelationDO>()
                .eq(UserRelationDO::getAppKey, request.getAppKey())
                .eq(UserRelationDO::getParentUserId, request.getUserId())
                .eq(request.getGrade() != null, UserRelationDO::getGrade, request.getGrade())
                .eq(request.getLevel() != null, UserRelationDO::getLevel, request.getLevel());
        List<UserRelationDO> userRelationList;
        // 按时间排序
        if ("time".equals(request.getOrderBy())) {
            userRelationList = baseMapper.selectList(userRelationDOLambdaQueryWrapper.orderByDesc(UserRelationDO::getCreateTime));
        } else {
            userRelationList = baseMapper.selectList(userRelationDOLambdaQueryWrapper.orderByDesc(UserRelationDO::getId));
        }

        if (CollectionUtils.isEmpty(userRelationList)) {
            return Collections.emptyList();
        }
        List<Long> userIds = userRelationList.stream().map(UserRelationDO::getUserId).collect(Collectors.toList());
        List<UserBaseDO> userBaseList = userBaseMapper.selectBatchIds(userIds);
        Map<Long, UserBaseDO> userMap = userBaseList.stream()
                .collect(Collectors.toMap(UserBaseDO::getId, Function.identity()));

        List<UserRelationListResp> responseList = new ArrayList<>();
        for (UserRelationDO userRelation : userRelationList) {
            UserRelationListResp response = BeansUtils.map(userRelation, UserRelationListResp.class);
            // 查询粉丝的粉丝数
            List<UserRelationDO> fansUserRelationList = baseMapper.selectList(new LambdaQueryWrapper<UserRelationDO>()
                    .eq(UserRelationDO::getAppKey, request.getAppKey())
                    .eq(UserRelationDO::getParentUserId, userRelation.getUserId()));
            // 设置粉丝数量
            response.setFansNumber(fansUserRelationList.size());
            response.setUserId(userRelation.getUserId());
            response.setLockFanTime(userRelation.getCreateTime());
            UserBaseDO userBaseDO = userMap.get(response.getUserId());
            if (userBaseDO != null) {
                response.setAvatarUrl(userBaseDO.getAvatarUrl());
                response.setNickName(userBaseDO.getNickName());
                response.setPhone(userBaseDO.getPhone());
            }
            responseList.add(response);
        }
        // 按热度排序
        if ("heat".equals(request.getOrderBy())) {
            responseList.sort((o1, o2) -> Integer.compare(o2.getFansNumber(), o1.getFansNumber()));
        }
        return responseList;
    }
}
